package com.panshi.controller;

import com.panshi.api.SystemUserApi;
import com.panshi.po.SystemUserPo;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class UserController {

    public List<InitMainVO> init(){
        List<InitMainVO> mapList = new ArrayList<>();
        //网站管理
        List<FunctionVO> website = new ArrayList<>();
        website.add(new FunctionVO("友链管理",1001,""));
        website.add(new FunctionVO("关于我们",1002,""));
        website.add(new FunctionVO("Banner",1003,""));
        website.add(new FunctionVO("配置信息",1004,""));
        mapList.add((new InitMainVO("网站管理",website)));
        //资讯
        List<FunctionVO> zx = new ArrayList<>();
        zx.add(new FunctionVO("资讯分类",20010,""));
        zx.add(new FunctionVO("资讯信息",20020,""));
        zx.add(new FunctionVO("资讯评论",20030,""));
        mapList.add(new InitMainVO("资讯管理",zx));
        //文章
        List<FunctionVO> wz = new ArrayList<>();
        wz.add(new FunctionVO("文章信息",3001,""));
        wz.add(new FunctionVO("文章审核",3002,""));
        mapList.add(new InitMainVO("文章管理",wz));
        //问答管理
        List<FunctionVO> wd = new ArrayList<>();
        wd.add(new FunctionVO("问题管理",4001,""));
        wd.add(new FunctionVO("提问审核",4002,""));
        wd.add(new FunctionVO("回答问题",4003,""));
        wd.add(new FunctionVO("回答评论",4004,""));
        mapList.add(new InitMainVO("问答管理",wd));
        //用户管理
        List<FunctionVO> user = new ArrayList<>();
        user.add(new FunctionVO("用户管理",5001,""));
        user.add(new FunctionVO("用户留言",5002,""));
        mapList.add(new InitMainVO("用户管理",user));
        //系统管理
        List<FunctionVO> sysUser = new ArrayList<>();
        sysUser.add(new FunctionVO("系统用户",60010,"sys/user-all"));
        mapList.add(new InitMainVO("系统管理",sysUser));

        return mapList;
    }

    @Resource
    private SystemUserApi systemUserApi;

    @GetMapping("/main-news")
    public ModelAndView queryAll(){

        ModelAndView mav = new ModelAndView("main/main");

        List<InitMainVO> mapList = init();

        mav.addObject("list",mapList);

        return mav;
    }

    @GetMapping("/sys/user-all")
    public String query(){

        return "layuiIndex/user-query";
    }

    @PostMapping("/sys/user-all")
    @ResponseBody
    public PageModel<SystemUserPo> queryAllSys(PageModel<SystemUserPo> pm){
        pm.init();
        log.info("pm ------------------------{}",pm);
        MybatisPlusPageVo pageVo = new MybatisPlusPageVo();
        pageVo.setCurrentPage(pm.getCurrentPage());
        pageVo.setPageSize(pm.getPageSize());
        List<SystemUserPo> list = systemUserApi.queryAll(pageVo).getData();
        log.info("list ------------------------{}",list);
        //总条数
        Integer count = systemUserApi.userTotal().getData();
        pm.setCount(count);
        pm.setList(list);
        return pm;
    }

    @PostMapping("/sys/user-vague/{vague}")
    @ResponseBody
    public PageModel<SystemUserPo> queryVague(PageModel<SystemUserPo> pm, @PathVariable String vague){
        pm.init();
        log.info("pm ------------------------{}",pm);
        SystemUserLikeQueryVo pageVo = new SystemUserLikeQueryVo();
        pageVo.setCurrentPage(pm.getCurrentPage());
        pageVo.setPageSize(pm.getPageSize());
        pageVo.setUsername(vague);
        List<SystemUserPo> list = systemUserApi.likeUserName(pageVo).getData();
        log.info("list ------------------------{}",list);

        Integer count = systemUserApi.likeNameTotal(vague).getData();
        pm.setCount(count);
        pm.setList(list);
        return pm;
    }

    @PostMapping("/sys/user-state/{state}")
    @ResponseBody
    public PageModel<SystemUserPo> queryState(PageModel<SystemUserPo> pm, @PathVariable Integer state){
        pm.init();
        log.info("pm ------------------------{}",pm);
        StateQuery pageVo = new StateQuery();
        pageVo.setCurrentPage(pm.getCurrentPage());
        pageVo.setPageSize(pm.getPageSize());
        pageVo.setState(state);
        List<SystemUserPo> list = systemUserApi.listStateUser(pageVo).getData();
        log.info("list ------------------------{}",list);

        pageVo.setCurrentPage(1);
        pageVo.setPageSize(100);
        Integer count = systemUserApi.stateCount(state).getData();
        pm.setCount(count);
        pm.setList(list);
        return pm;
    }

}
